package com.cjw.campus.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjw.campus.system.entity.MatriculationYearTable;
import com.cjw.campus.system.service.IMatriculationYearTableService;
import com.cjw.campus.system.vo.MatriculationYearTableVO;
import core.result.JsonResult;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 入学年份表 前端控制器
 * </p>
 *
 * @author cjw
 * @since 2022-11-21
 */
@RestController
@RequestMapping("/system/matriculationYearTable")
@Api(tags = "年级管理", value = "年级管理接口")
public class MatriculationYearTableController {

    @Autowired
    private IMatriculationYearTableService matriculationYearTableService;

    @PostMapping("/Save")
    @ApiOperation(value = "保存年级信息", notes = "保存年级信息", httpMethod = "POST")
    @ApiParam(name = "matriculationYearTable", value = "年级实体类", required = true)
    public JsonResult save (MatriculationYearTable matriculationYearTable) {
        // 保存年级信息
        boolean save = matriculationYearTableService.save(matriculationYearTable);
        // 判断年级是否新增成功
        if (save) {
            // 成功返回
            return JsonResult.ok();
        }
        // 失败返回
        return JsonResult.error("新增失败");
    }

    @PostMapping("/updateById")
    @ApiOperation(value = "根据id更新年级信息", notes = "根据id更新年级信息", httpMethod = "POST")
    @ApiParam(name = "matriculationYearTable", value = "年级实体类", required = true)
    public JsonResult updateById (MatriculationYearTable matriculationYearTable) {
        boolean updateById = matriculationYearTableService.updateById(matriculationYearTable);
        // 判断用户是否修改成功
        if (updateById) {
            // 成功返回
            return JsonResult.ok();
        }
        // 失败返回
        return JsonResult.error("修改失败");
    }

    @GetMapping("/removeById")
    @ApiOperation(value = "根据id删除年级信息", notes = "根据id删除年级信息", httpMethod = "GET")
    @ApiParam(name = "matriculationYearId", value = "年级ID", required = true)
    public JsonResult removeById (Long matriculationYearId) {
        boolean removeId = matriculationYearTableService.removeById(matriculationYearId);
//         判断用户是否删除成功
        if (removeId) {
            // 成功返回
            return JsonResult.ok();
        }
        // 失败返回
        return JsonResult.error("删除失败");
    }

    @PostMapping ("/removeByIds")
    @ApiOperation(value = "根据id批量删除年级信息", notes = "根据id批量删除年级信息", httpMethod = "POST")
    @ApiParam(name = "matriculationYearId", value = "年级ID", required = true)
    public JsonResult removeByIds (Long[] idList) {
        boolean removeSchoolIds = matriculationYearTableService.removeByIds(Arrays.asList(idList));
        if (removeSchoolIds) {
            // 成功返回
            return JsonResult.ok();
        }
        // 失败返回
        return JsonResult.error("删除失败");
    }

    @GetMapping("/listPageByYear")
    @ApiOperation(value = "分页查询年级信息", notes = "分页查询年级信息", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "当前页", required = true, dataType = "Integer", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "页数", required = true, dataType = "Integer", defaultValue = "2")
    })
    public JsonResult listPageByYear (Integer currentPage,Integer pageSize) {
        IPage<MatriculationYearTableVO> page = new Page<>(currentPage, pageSize);
        IPage<MatriculationYearTableVO> listPageByYear = matriculationYearTableService.listPageByYear(page);
        return JsonResult.ok(listPageByYear);
    }

    @GetMapping("/listPageByYearCodeAndName")
    @ApiOperation(value = "模糊分页查询年级信息", notes = "模糊分页查询年级信息", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "当前页", required = true, dataType = "Integer", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "页数", required = true, dataType = "Integer", defaultValue = "2"),
            @ApiImplicitParam(name = "matriculationYearCode", value = "年级编码", dataType = "String"),
            @ApiImplicitParam(name = "matriculationYearName", value = "年级名称", dataType = "String"),
    })
    public JsonResult listPageByYearCodeAndName (Integer currentPage,Integer pageSize,String matriculationYearCode, String matriculationYearName) {
        IPage<MatriculationYearTableVO> page = new Page<>(currentPage, pageSize);
        IPage<MatriculationYearTableVO> mapPage = matriculationYearTableService.listPageByYearCodeAndName(page, matriculationYearCode, matriculationYearName);
        return JsonResult.ok(mapPage);
    }

    @GetMapping("/listByYearId")
    @ApiOperation(value = "根据id查询年级信息", notes = "根据id查询年级信息", httpMethod = "GET")
    @ApiParam(name = "matriculationYearId", value = "学校ID", required = true)
    public JsonResult listByYearId (Long matriculationYearId) {
        MatriculationYearTableVO listByYearId = matriculationYearTableService.listByYearId(matriculationYearId);
        return JsonResult.ok(listByYearId);
    }

    @GetMapping("/list")
    @ApiOperation(value = "查询所有年级信息", notes = "查询所有年级信息", httpMethod = "GET")
    public JsonResult list () {
        List<MatriculationYearTable> list = matriculationYearTableService.list();
        return JsonResult.ok(list);
    }

}
